package org.mockito.learningtests;

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.test.AndroidTestCase;

public class ExceptionTests extends AndroidTestCase {

	public void testVoidCheckedException() {
		ClassThrowingExceptions testClass = mock(ClassThrowingExceptions.class);

		try {
			try {
				doThrow(new Exception()).when(testClass).methodWithException();
			} catch (Exception ex) {
				fail("Mocking setup isn't supposed to throw exceptions!");
			}

			testClass.methodWithException();

			fail("methodWithException() was supposed to throw an exception.");
		} catch (Exception e) {
		}
	}

	public void testUncheckedException() {
		ClassThrowingExceptions testClass = mock(ClassThrowingExceptions.class);

		try {

			when(testClass.methodWithUncheckedException()).thenThrow(new RuntimeException());
			testClass.methodWithUncheckedException();

			fail("methodWithUncheckedException() was supposed to throw an exception.");
		} catch (Exception e) {
		}
	}

}
